﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.SharePoint.Configuration;
using System.Reflection;
using Microsoft.Office.Server.ActivityFeed;
using System.Configuration;
using System.Web.Configuration;
using ESF.SharePoint.ActivityAggregators.Providers;

namespace ESF.SharePoint.Configuration
{
    public class ActivityAggregatorProviderManager
    {

        static ActivityAggregatorProviderManager()
        {
            Initialize();
        }

        private static ActivityAggregationProviderCollection _providerCollection;
        
        /// <summary>
        /// Returns the provider collection
        /// </summary>
        public static ActivityAggregationProviderCollection Providers
        {
            get { return _providerCollection; }
        }

        private static ProviderSettingsCollection _providerSettings;
        public static ProviderSettingsCollection ProviderSettings 
        {
            get { return _providerSettings; }
        }

        //private static ActivityTemplateCollection _activityTemplates;

        ///// <summary>
        ///// Returns the provider collection
        ///// </summary>
        //public static ActivityTemplateCollection ActivityTemplates
        //{
        //    get { return _activityTemplates; }
        //}

        /// <summary>
        ///Reads the configuration related to the set of configured
        ///providers and sets the default and collection of providers and settings.
        /// </summary>
        private static void Initialize()
        {
            ActivityAggregatorSection configSection = (ActivityAggregatorSection)ConfigurationManager.GetSection("ActivityAggregator");
            if(configSection == null)
                throw new ConfigurationErrorsException("Data provider section is not set.");

            _providerCollection = new ActivityAggregationProviderCollection();
            ProvidersHelper.InstantiateProviders(configSection.Providers, _providerCollection, typeof(IActivityAggregatorProvider));
            _providerSettings = configSection.Providers;
            //_activityTemplates = configSection.ActivityTemplates;
            //ConfigurationElement element = null;
        }

    }
}
